package frame;

import pojo.UserDO;
import service.UserService;
import service.impl.UserServiceImpl;
import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetPanel extends JPanel implements ActionListener {
    private ATM atm;
    private JButton sureBtn;
    private JButton clearBtn;
    private Icon sureImg = Util.getImageIcon("/images/buttonSure.jpg");
    private Icon clearImg = Util.getImageIcon("/images/buttonOff.jpg");
    private JLabel get = new JLabel("请输入取款金额：");
    private JTextField getmoney = new JTextField();

    private UserService userService = new UserServiceImpl();

    public GetPanel(ATM atm) {
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        get.setBounds(200, 200, 200, 30);
        get.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(get);
        getmoney.setBounds(420, 200, 200, 30);
        this.add(getmoney);

        sureBtn = new JButton(sureImg);
        sureBtn.setBounds(200, 240, 140, 50);
        this.add(sureBtn);
        sureBtn.addActionListener(this);

        clearBtn = new JButton(clearImg);
        clearBtn.setBounds(420, 240, 140, 50);
        this.add(clearBtn);
        clearBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//        Util db = new Util();
//        UserDO loginUser = db.getById(atm.getLoginUser().getId());
//        double balance=Double.valueOf(getmoney.getText());
//        if (e.getSource() == sureBtn) {
//            if (Double.valueOf(getmoney.getText()) > loginUser.getMoney()) {
//                JOptionPane.showMessageDialog(null, "你没有那么多钱，请重新输入");
//            } else {
//                JOptionPane.showMessageDialog(null, "本次取款金额为：" + getmoney.getText() + "元，余额为：" + (loginUser.getMoney()-balance));
////                atm.getUsers().get(atm.getNum()).setMoney(atm.getUsers().get(atm.getNum()).getMoney() - Double.valueOf(getmoney.getText()));
////                String sql="update t_user set u_money="+atm.getUsers().get(atm.getNum()).getMoney()+"where u_name='"+atm.getUsers().get(atm.getNum()).getUsername()+"'";
////                Util.insert(sql);
//                loginUser.setMoney(loginUser.getMoney()-balance);
//                db.update(loginUser);
//                atm.switchPanel("menu");
//            }
//
//        } else if (e.getSource() == clearBtn) {
//            atm.switchPanel("menu");
//        }
//    }

        if (e.getSource() == sureBtn) {
            try {
                double balance=Double.valueOf(getmoney.getText());
                userService.getmoney(atm.getLoginUser().getId(),balance);
                JOptionPane.showMessageDialog(this,"取款成功");
                atm.switchPanel("menu");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this,exception.getMessage());
            }
        }else if (e.getSource() == clearBtn) {
            atm.switchPanel("menu");
        }
    }
}
